package com.bj.jjz.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bj.jjz.R;
import com.bj.jjz.base.BaseFragment;
import com.bj.jjz.entity.WeatherWithNo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author Hu
 * @Time 2017/4/14 --10:59.
 * @Email 87683202@qq.com
 */
public class HomeFragment extends BaseFragment {
    View rootView;
    private static final String TAG = "HomeFragment";
    Unbinder unbinder;
    @BindView(R.id.today_control)
    TextView todayControl;
    @BindView(R.id.tomorrow_control)
    TextView tomorrowControl;
    @BindView(R.id.today_weather)
    TextView todayWeather;
    @BindView(R.id.today_wash_car)
    TextView todayWashCar;
    WeatherWithNo weatherNo;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.e(TAG, "onCreateView=====");
        if (null == rootView) {
            rootView = View.inflate(getContext(), R.layout.fragment_home, null);
        }
        unbinder = ButterKnife.bind(this, rootView);
        isReady = true;
        setUrl("https://api.accident.zhongchebaolian.com");
        lazyLoad();
        return rootView;
    }

    @Override
    protected void lazyLoad() {
        Log.e(TAG + "IS==", isCanDo() + "");
        if (isCanDo())
            getData();
    }

    private void getData() {

        Call<WeatherWithNo> stringCall = requestServes.getWeatherWithNo("1101", 116.608809f, 39.890864f, "4c95082984034c8996b967e0d2b95f83");
        stringCall.enqueue(new Callback<WeatherWithNo>() {
            @Override
            public void onResponse(Call<WeatherWithNo> call, Response<WeatherWithNo> response) {
                weatherNo=response.body();
                setDate();
            }

            @Override
            public void onFailure(Call<WeatherWithNo> call, Throwable t) {
                Log.e(TAG, t.toString());
            }
        });

    }

    private void setDate() {
        if (weatherNo!=null){
            todayControl.setText("今日限行："+weatherNo.getAstrictno());
            tomorrowControl.setText("明日限行："+weatherNo.getAstrictmt());
            todayWeather.setText(weatherNo.getWeatherdes()+weatherNo.getWeathersection());
            todayWashCar.setText(weatherNo.getWashcar()+"洗车");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
